package gui.components;

import java.awt.AlphaComposite;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class MImage extends JLabel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3563421283901225213L;
	ImageIcon picture;

	private MImage() {
		super();
		this.setText(null);
		this.setBorder(null);
		this.setVisible(true);
	}

	public MImage(String filePath) {
		this();
		ImageIcon picture = new ImageIcon(filePath);
		int hheight = picture.getIconHeight();
		int wwidth = picture.getIconWidth();
		this.setIcon(picture);
		this.setSize(wwidth, hheight);
	}
	
	public MImage(ImageIcon picture) {
		this();
		int hheight = picture.getIconHeight();
		int wwidth = picture.getIconWidth();
		this.setIcon(picture);
		this.setSize(wwidth, hheight);
	}

	public MImage(String filePath, Dimension newSize, boolean keepRatio)
			throws IOException {
		this(ImageIO.read(new File(filePath)),newSize,keepRatio);
	}
	
	public MImage(Image i, Dimension newSize, boolean keepRatio){
		this();

		if (keepRatio) {
			int width = i.getWidth(null);
			int height = i.getHeight(null);
			double ratioW = (double) newSize.getWidth() / width;
			double ratioH = (double) newSize.getHeight() / height;
			int newWidth, newHeight;
			if (ratioW > 1 && ratioH > 1) {
				if (ratioW < ratioH) {
					newWidth = (int) newSize.getWidth();
					newHeight = (int) (height * ratioW);
				} else {
					newWidth = (int) (width * ratioH);
					newHeight = (int) newSize.getHeight();
				}
			} else {
				if (ratioW < ratioH) {
					newWidth = (int) newSize.getWidth();
					newHeight = (int) (height * ratioW);
				} else {
					newWidth = (int) (width * ratioH);
					newHeight = (int) newSize.getHeight();
				}

			}
			/*
			 * if (newSize.getWidth() >= newSize.getHeight()) { newHeight =
			 * (int) newSize.getHeight(); newWidth = (int) (newHeight * ratio);
			 * } else { newWidth = (int) newSize.getWidth(); newHeight = (int)
			 * (newWidth / ratio); }
			 */
			picture = new ImageIcon(resizeImage(i, newWidth, newHeight));
		} else {
			picture = new ImageIcon(resizeImage(i, (int) newSize.getWidth(),
					(int) newSize.getHeight()));
		}

		int hheight = picture.getIconHeight();
		int wwidth = picture.getIconWidth();
		this.setIcon(picture);
		this.setSize(wwidth, hheight);

	}

	public static BufferedImage resizeImage(final Image image, int width,
			int height) {
		final BufferedImage bufferedImage = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		final Graphics2D graphics2D = bufferedImage.createGraphics();
		graphics2D.setComposite(AlphaComposite.Src);
		graphics2D.drawImage(image, 0, 0, width, height, null);
		graphics2D.dispose();

		return bufferedImage;
	}
	
	
	public static Image rotateImage(Image im,int degrees){
		while(degrees>0){
			im = rotateImage90(im,90);
			degrees-=90;
		} 
		return im;
	}
	
	private static Image rotateImage90(Image im,double degrees){
		Image rotatedImage = new BufferedImage(im.getHeight(null), im.getWidth(null), BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2d = (Graphics2D) rotatedImage.getGraphics();
	    g2d.rotate(Math.toRadians(90));
	    g2d.drawImage(im, 0, -rotatedImage.getWidth(null), null);
	    g2d.dispose();
	    return rotatedImage;
	}

}
